package com.jams.goodox.activity.serve;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.jams.goodox.BaseFragment;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.constant.AppConfig;
import com.jams.goodox.constant.H5UrlConstants;
import com.jams.goodox.utils.DensityUtil;
import com.jams.goodox.utils.StringUtils;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

public class TownIntroduceH5Fragment extends BaseFragment {

    @Bind(R.id.webView_town_introduce)
    WebView mWebView;
    private String type;

    public TownIntroduceH5Fragment() {
        // Required empty public constructor
    }


    public static TownIntroduceH5Fragment newInstance(String url) {
        TownIntroduceH5Fragment fragment = new TownIntroduceH5Fragment();
        Bundle args = new Bundle();
        args.putString("url", url);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getString("url");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_town_introduce_h5, container, false);
        ButterKnife.bind(this, view);
        //增加headView
        View v = new View(mContext);
        v.setBackgroundColor(getResources().getColor(R.color.gray_background));
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtil.dp2px(mContext, 8));
        mWebView.addView(v, 0, params);
        HashMap<String, String> urlParams = new HashMap<>();
        urlParams.put("token", SePref.getToken());
        urlParams.put("mobileType", AppConfig.MOBILE_TYPE);
        urlParams.put("companyCode", AppConfig.COMPANY_CODE);
        urlParams.put("townId", SePref.getCommunityId() + "");
        final String url = StringUtils.getUrl(StringUtils.newString(type), urlParams);
        mWebView.loadUrl(url);
        WebSettings webSettings = mWebView.getSettings();
        if(H5UrlConstants.map.equals(type)){
            //***地图webview滚动优先
            mWebView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    //AppConfig.getWebPageBaseUrl()
                    if (event.getAction() == MotionEvent.ACTION_UP)
                        mWebView.requestDisallowInterceptTouchEvent(false);
                    else
                        mWebView.requestDisallowInterceptTouchEvent(true);
                    return false;
                }
            });
        }
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
         
        webSettings.setJavaScriptEnabled(true);
        return view;

    }


}
